<?php
class Control
{
	var $input;
	var $args;
	var $output;
	var $control;
	var $parameters;
	var $module;
	var $Config;
	
	var $page;
	
	function Control($input, $args, &$page)
	{
		global $Config;
		$this->Config = &$Config;

		$this->input = $input;
		$this->args = $args;
		$this->output = '';
		
		$this->page = &$page;
		
		// extract attributes/parameters
		$pattern = '|(?:<*?)(?P<prefix>\w*)(?::*?)(?P<module>\w*)(?:\s+)(?:(?P<key>\w+)(?:="(?P<value>[^"]*)")?)*(?:[/>]*?)|';
		$result = preg_match_all($pattern, $this->input, $matches, PREG_SET_ORDER);
		$this->module = $matches[0]['module'];

//if ($this->page->IsDebug) { print('<pre>'.var_export($matches, true).'</pre>');exit; }
//print('<pre>'.var_export($matches, true).'</pre>');exit;
//if ($this->page->IsDebug) {print('<pre>'.var_export($this, true).'</pre>');exit; }
//if ($this->page->IsTest)
//{
//if ($this->page->IsDebug) { print('<pre>'.var_export($this->parameters, true).'</pre>');exit; }
		$this->parameters = array();
		$i = 0;
		foreach ($matches as $key=>$match)
		{
			if (isset($match['key']) && $match['key'] != '' && isset($match['value']))
			{
				$this->parameters[$match['key']][] = $match['value'];
			}
		}
//if ($this->page->IsDebug) {print('<pre>'.var_export($this->parameters, true).'</pre>');exit; }
//}
/*else
{
		foreach ($matches as $key=>$match)
		{
			if (isset($match['key']) && $match['key'] != '' && isset($match['value']) && $match['value'] != '')
			{
				$this->parameters[$match['key']] = $match['value'];
			}
		}
}*/

		// instantiate control
		// & Execute control-command on control
		$moduleFile = $this->Config->SiteRoot.'Controls/'.$this->module.'.php';
		if (!file_exists($moduleFile)) // Allow override on site-level
		{
			$moduleFile = $this->Config->WebCMSRoot.'Controls/'.$this->module.'.php';
			if (!file_exists($moduleFile))
			{
				//die ('The module "'.$this->module.'" does not exist.');
				$page->Error404('The module "'.$this->module.'" does not exist.');
			}
		}

		require_once($moduleFile);

		if (class_exists($this->module))
		{
			$mod = $this->module;
			eval('$this->module = new '.$mod.'($this->parameters, &$this->page);');
			eval('$'.$mod.' = &$this->module;');
			eval('global $'.$mod.';');
		}
		else
		{
			$page->Error404('The module "'.$this->module.'" does not exist.');
		}

		$this->output = $this->module->output;
	}
	
}
?>